package com.cgy.utils.base.type;

import java.util.List;
import java.util.Map;

import com.cgy.utils.base.list.ListHelper;
import com.cgy.utils.base.map.MapHelper;
import com.cgy.utils.base.objects.ObjectHelper;
import com.cgy.utils.base.reflect.ReflectHelper;
import com.cgy.utils.base.strings.StringHelper;

/**
 * type常量
 * @author CHTK
 */
public abstract class TypeHelper {

    public static final String STRING = "string";
    public static final String INTEGER = "int";
    public static final String BYTE = "byte";
    public static final String FLOAT = "float";
    public static final String DOUBLE = "double";
    public static final String SHORT = "short";
    public static final String BOOLEAN = "boolean";
    public static final String CHAR = "char";
    public static final String LONG = "long";
    public static final String VOID = "void";

    @SuppressWarnings("unchecked")
	public static final Class<Map<String, String>> MAP_STRING_STRING = (Class<Map<String, String>>) MapHelper.newMapStringString().getClass();
    @SuppressWarnings("unchecked")
	public static final Class<Map<String, Object>> MAP_STRING_OBJECT = (Class<Map<String, Object>>) MapHelper.newMapStringObject().getClass();
    @SuppressWarnings("unchecked")
    public static final Class<List<String>> LIST_STRING = (Class<List<String>>) ListHelper.newArrayList().getClass();


    /**
     * 类型转化
     * @param object
     * @param tClass
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
	public static <T>T conversion(Object object, Class<T> tClass) {
        String name = tClass.getSimpleName();
        if(STRING.equals(name)) {
            return (T) ObjectHelper.toString(object);
        } else if(FLOAT.equals(name)) {
            return (T) ObjectHelper.toFloat(object);
        } else if(LONG.equals(name)) {
            return (T) ObjectHelper.toLong(object);
        } else if(DOUBLE.equals(name)) {
            return (T) ObjectHelper.toDouble(object);
        } else if(SHORT.equals(name)) {
            return (T) ObjectHelper.toString(object);
        } else if(BOOLEAN.equals(name)) {
            return (T) ObjectHelper.toBoolean(object);
        } else if(INTEGER.equals(name)) {
            return (T) ObjectHelper.toInt(object);
        } else if(BYTE.equals(name)) {
            return (T) ObjectHelper.toByte(object);
        } else if(CHAR.equals(name)) {
            return (T) (Character)object;
        }

        return null;
    }
    /**
     * 判断字段类型
     * @param value
     * @return
     */
    public static boolean isVoid(final Object value) {
        return null == value;
    }
    /**
     * 判断字段类型
     * @param value
     * @return
     */
    public static boolean isThing(final Object value, final String className) {
        if(isVoid(value) || StringHelper.isBlank(className)) {
            return false;
        }

        String simpleName = value.getClass().getSimpleName();
        return className.equals(simpleName);
    }
    /**
     * 判断字段类型
     * @param value
     * @return
     */
    public static boolean isString(final Object value) {
        return isThing(value, ReflectHelper.STRING);
    }
    /**
     * 判断字段类型
     * @param value
     * @return
     */
    public static boolean isInt(final Object value) {
        return isThing(value, ReflectHelper.INT);
    }
    /**
     * 判断字段类型
     * @param value
     * @return
     */
    public static boolean isLong(final Object value) {
        return isThing(value, ReflectHelper.LONG);
    }
    /**
     * 判断字段类型
     * @param value
     * @return
     */
    public static boolean isShort(final Object value) {
        return isThing(value, ReflectHelper.SHORT);
    }
    /**
     * 判断字段类型
     * @param value
     * @return
     */
    public static boolean isByte(final Object value) {
        return isThing(value, ReflectHelper.BYTE);
    }
    /**
     * 判断字段类型
     * @param value
     * @return
     */
    public static boolean isBoolean(final Object value) {
        return isThing(value, ReflectHelper.BOOLEAN);
    }
    /**
     * 判断字段类型
     * @param value
     * @return
     */
    public static boolean isFloat(final Object value) {
        return isThing(value, ReflectHelper.FLOAT);
    }
    /**
     * 判断字段类型
     * @param value
     * @return
     */
    public static boolean isDouble(final Object value) {
        return isThing(value, ReflectHelper.DOUBLE);
    }
    /**
     * 判断字段类型
     * @param value
     * @return
     */
    public static boolean isNumber(final Object value) {
        return isFloat(value) | isDouble(value) | isInt(value) | isShort(value);
    }
}
